package com.zh.chat.service;

import java.io.IOException;

import org.apache.log4j.Logger;

import com.zh.chat.controller.ClientContext;
import com.zh.chat.entity.UDPProtocol;
import com.zh.chat.entity.User;
import com.zh.chat.gui.ChatFrame;
import com.zh.chat.net.FileClient;
import com.zh.chat.net.TCPDTO;
import com.zh.chat.net.UdpDTO;

public class NetService {
	private Logger log = Logger.getLogger(NetService.class);
	private ClientContext clientContext = null;
	private UdpDTO udpDTO = null;
	private TCPDTO tcpDTO = null;

	public void setClientContext(ClientContext clientContext) {
		this.clientContext = clientContext;
	}

	public void setNetDTO(UdpDTO netDTO) {
		this.udpDTO = netDTO;
	}
	
	public void setTcpDTO(TCPDTO tcpDTO) {
		this.tcpDTO = tcpDTO;
	}
	public TCPDTO getTcpDTO() {
		return tcpDTO;
	}
	public int getVOICE_IN_PORT(){
		return udpDTO.getVOICE_IN_PORT();
	}

	/**
	 * 登陆服务
	 * 
	 * @param id
	 *            用户名
	 * @param pwd
	 *            密码
	 * @param ip
	 *            服务器地址
	 * @param port
	 *            图片端口
	 * @return
	 * @throws IdOrPwdException
	 */
	public void login(Long id, String pwd, int status) {
		User user = null;
		UDPProtocol protocol = new UDPProtocol();
		protocol.setCode(UDPProtocol.REQ_LOGIN);
		protocol.setRequestId(id);
		protocol.setTargetId(0L);
		user = new User();
		user.setUserPwd(pwd);
		user.setSysId(id);
		user.setStatus(status);
		user.setClientPort(udpDTO.getClientRecvPort());
		user.setClientIp(udpDTO.getClientIp());
		user.setClientFilePort(udpDTO.getFileInPort());
		protocol.setClazz(user.getClass().getName());
		protocol.setData(user);
		try {
			udpDTO.send(protocol);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 请求服务器发送好友文件
	 * @param userInfo
	 */
	public void requestFriends(Long id) {
		UDPProtocol protocol = new UDPProtocol();
		protocol.setCode(UDPProtocol.REQ_FRIENDS_LIST);
		protocol.setRequestId(id);
		protocol.setTargetId(0L);
		try {
			udpDTO.send(protocol);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void send(UDPProtocol protocol){
		try {
			udpDTO.send(protocol);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void requestIp(){
		udpDTO.requestIP();
	}
	
	/**
	 * 交换targetId 和 requestId
	 * @param protocol
	 * @return
	 */
	public UDPProtocol reverse(UDPProtocol protocol){
		Long targetId = protocol.getRequestId();
		protocol.setRequestId(protocol.getTargetId());
		protocol.setTargetId(targetId);
		return protocol;
	}

	public UDPProtocol recive() {
		UDPProtocol recvProtocol = udpDTO.recive();// 等待服务器返回个人信息
		return recvProtocol;
	}
	public void close(){
		udpDTO.close();
	}
	
	/**
	 * 启动线程接收服务器发送的文件
	 */
	public void reciveFile(String filePath, UDPProtocol protocol){
		int port = (Integer) protocol.getData();
		try {
			new FileClient(filePath, protocol.getServerIp(), port);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setIP(String ip){
		udpDTO.setClientIP(ip);
		tcpDTO.setClientIP(ip);
	}
	
	public void setPort(int port){
		
	}

	public void readyVoice(java.lang.Long reqId,java.lang.Long tarId) {
		UDPProtocol protocol = new UDPProtocol();
		protocol.setCode(UDPProtocol.REQ_RECIVE_VOICE);
		protocol.setData(udpDTO.getVOICE_IN_PORT()+":"+udpDTO.getClientIp());
		ChatFrame frame = clientContext.getChatFrame(tarId);
		frame.setVOICE_IN_LOCAL_PORT(udpDTO.getVOICE_IN_PORT());
		protocol.setClazz("".getClass().getName());
		protocol.setRequestId(reqId);
		protocol.setTargetId(tarId);
		try {
			udpDTO.send(protocol);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
